package com.ht.medicaltreatment.activity.haocai2

import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemChildClickListener
import com.gym.baselib.base.BaseActivity
import com.hjq.http.EasyHttp
import com.hjq.http.listener.OnHttpListener
import com.ht.medicaltreatment.R
import com.ht.medicaltreatment.adapter.ApplySubjectRecordAdapter
import com.ht.medicaltreatment.bean.ApplyConsumableJSON
import com.ht.medicaltreatment.bean.ShenQingBean
import com.ht.medicaltreatment.bean.ShenQingProjectConsumable
import com.ht.medicaltreatment.http.api.ShenQingAPI
import com.ht.medicaltreatment.view.InputCountDialog2
import com.xgst.lib.utils.showToast
import kotlinx.android.synthetic.main.activity_apply_consumable_layout.recyclerView
import kotlinx.android.synthetic.main.activity_apply_consumable_layout.tvApply
import kotlinx.android.synthetic.main.activity_apply_consumable_layout.tvName
import kotlinx.android.synthetic.main.activity_apply_consumable_layout.tvProgress

class ApplyConsumableActivity:BaseActivity(), OnItemChildClickListener {

    override fun getLayoutId() = R.layout.activity_apply_consumable_layout
    private var id = 0
    private lateinit var adatper:ApplySubjectRecordAdapter

    override fun init() {
        setTitle("申请耗材")
        id = intent.getIntExtra("id",0)
        recyclerView.layoutManager = LinearLayoutManager(this)
        adatper = ApplySubjectRecordAdapter()
        adatper.setOnItemChildClickListener(this)
        recyclerView.adapter = adatper
        showProgress()
        EasyHttp.post(this)
            .api(ShenQingAPI("$id"))
            .request(object : OnHttpListener<ShenQingBean> {
                override fun onHttpSuccess(result: ShenQingBean) {
                    dismissProgress()
                    val list = result.data.projectConsumableList
                    adatper.setNewInstance(list)
                    tvProgress.text = "使用进度：${result.data.consumableUseProgress}"
                    tvName.text = "专题：${result.data.protocolNumber}"
                }

                override fun onHttpFail(e: Exception) {
                   dismissProgress()
                    showToast(e.message)
                }
            })

        tvApply.setOnClickListener {
            submit();
        }
    }

    private fun submit() {
        val list = arrayListOf<ApplyConsumableJSON.ProjectConsumableRecordDetailsListBean>()

        adatper.data.forEach {
            if(it.useNumber > 0){
                val bean = ApplyConsumableJSON.ProjectConsumableRecordDetailsListBean()
                bean.useNumber = it.useNumber
                bean.projectConsumableId = it.id
                list += bean
            }
        }
        if(list.size == 0){
            return
        }
        showProgress()
        val json = ApplyConsumableJSON()
        json.projectId = id
        json.projectConsumableRecordDetailsList = list
        EasyHttp.post(this)
            .api(json)
            .request(object : OnHttpListener<ShenQingBean> {
                override fun onHttpSuccess(result: ShenQingBean) {
                    dismissProgress()
                    showToast("申请成功")
                    finish()
                }

                override fun onHttpFail(e: Exception) {
                    dismissProgress()
                    showToast(e.message)
                }
            })
    }

    override fun onItemChildClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        val item = adapter.getItem(position) as ShenQingProjectConsumable
        InputCountDialog2(this,item.number,item.useNumber).showDialog {
            (adapter.data[position] as ShenQingProjectConsumable).useNumber = it
            adapter.notifyItemChanged(position)
        }
    }
}